﻿using Commonality.RedisHelper.Service;
using Commonality.ReturnView;
using IService.PlatApi;
using Models.Managemen;
using ModelToDto.PlatAPI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Dm.net.buffer.ByteArrayBuffer;

namespace Service.PlatApi
{
    public class LoginVerification:BaseService,ILoginVerification
    {
        private static readonly string KEY_PREFIX = "user:verify:code:";
        public ISqlSugarClient sqlSugarClient { get; set; }
        RedisStringService redisStringService { get; set; }
        public LoginVerification(ISqlSugarClient sqlSugarClient, RedisStringService redisStringService) :base(sqlSugarClient)
        { 
         this .sqlSugarClient=sqlSugarClient;
            this.redisStringService= redisStringService;
        }

        ApiResult ILoginVerification.RegiestPhoneNum(RegiesteUserDTO regiesteUserDTO)
        {
            var key = KEY_PREFIX + regiesteUserDTO.mobile;
            var getUserVcode = redisStringService.Get(key );

            //验证手机号是否合法
            if (string.IsNullOrEmpty(regiesteUserDTO.mobile) || regiesteUserDTO.mobile.Length != 11|| string.IsNullOrEmpty(regiesteUserDTO.password)||string.IsNullOrEmpty(regiesteUserDTO.verificationCode))
            {
                return new ApiResult { Success = false, apiResultStatus = ApiResultStatus.Failure, Message = "输入信息不合法" };
            }
            if (getUserVcode == null)
            {
               
                return new ApiResult { Success = false, apiResultStatus = ApiResultStatus.Success, Message="验证码已过期，请重新获取"};
            }
           
            else
            {
                if (JsonConvert.DeserializeObject<string>(getUserVcode) == regiesteUserDTO.verificationCode)
                {
                    var user = sqlSugarClient.Queryable<Mp_Users>().Where(x => x.Phone == regiesteUserDTO.mobile).First();
                    if (user != null)
                    {
                        return new ApiResult { Success = false, apiResultStatus = ApiResultStatus.Failure, Message = "手机号已被占用" };
                    }
                   var insertResult= base.Insert(new Mp_Users { Phone = regiesteUserDTO.mobile, Password = regiesteUserDTO.password ,UserType=(int)UserTypeEnum.Ordinary,Name=regiesteUserDTO.mobile });
                    if (insertResult > 0)
                    {
                        return new ApiResult { Success = true, apiResultStatus = ApiResultStatus.Failure };
                    }
                   
                }
                
            }
            return new ApiResult { Success = false, apiResultStatus = ApiResultStatus.Failure, Message = "注册失败" };


        }

        ApiResult ILoginVerification.VerifCode(string phoneNum)
        {
            var key = KEY_PREFIX + phoneNum;
            var getUserVcode= redisStringService.Get(key + "1ML");

            //验证手机号是否合法
            if (string.IsNullOrEmpty(phoneNum) || phoneNum.Length != 11)
            {
                return new ApiResult { Success = false, apiResultStatus = ApiResultStatus.Failure, Message = "手机号不合法" };
            }
            if (getUserVcode == null)
            {
                Random random = new Random();
                string code = random.Next(100000, 999999).ToString();// 生成随机6位数字验证码
                 //验证手机号是否存在
                var user = sqlSugarClient.Queryable<Mp_Users>().Where(x => x.Phone == phoneNum).First();
                if (user != null)
                {
                    return new ApiResult { Success = false, apiResultStatus = ApiResultStatus.Failure, Message = "手机号已被占用" };
                }
                redisStringService.Set(key + "1ML", code, TimeSpan.FromMinutes(1));
                redisStringService.Set(key, code, TimeSpan.FromMinutes(5));
                //发送短信   ----将验证码发送到用户手机
                return new ApiResult { Success = true, apiResultStatus = ApiResultStatus.Success,Data=code };
            }
            else
            {
                
                return new ApiResult { Success = false, apiResultStatus = ApiResultStatus.Failure, Message = "验证码已经发送请1分钟后重新获取" };
            }
        }
    }
}
